//
//  LoginPhoneViewController.m
//  sklive
//
//  Created by Macx on 2017/5/5.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "LoginPhoneViewController.h"

#import "TabBarViewController.h"
#import "RegisterViewController.h"
#import "LookingPassViewController.h"

#import "NSString+Common.h"
#import "LoginServer.h"

//#import "TCLoginParam.h"
//#import "TCLoginModel.h"

@interface LoginPhoneViewController () <UITextFieldDelegate>

@property (nonatomic, strong) UITabBarController *mainVC;

@end

@implementation LoginPhoneViewController {
    UITextField     *_phoneNum;
    UITextField     *_passWord;
    UIButton        *_keepBtn;
    UILabel         *_register;
    TCLoginParam    *_loginParam;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    // 先判断是否自动登录
//    BOOL isAutoLogin = [TCLoginModel isAutoLogin];
//    if (isAutoLogin) {
//        _loginParam = [TCLoginParam loadFromLocal];
//    }
//    else {
//        _loginParam = [[TCLoginParam alloc] init];
//    }
//    // 登录前需要先初始化IMSDK
//    [[TCLoginModel sharedInstance] initIMSDK];

    [self buildUI];
    
}

-(void)popAction{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark ==========构建UI==========
- (void)buildUI {
    
    self.navigationItem.title = @"登录";
    self.view.backgroundColor = kWhiteColor;
    
    UIButton *returnBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [returnBtn setImage:IMAGE(@"return") forState:UIControlStateNormal];
    [returnBtn addTarget:self action:@selector(popAction) forControlEvents:UIControlEventTouchUpInside];
    returnBtn.frame = CGRectMake(0, 0, 44, 44);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:returnBtn];
    
//    NSArray *logImaArr = @[@"icon_reg_num", @"icon_reg_pwd"];
//    NSArray *placeArr = @[@"请输入手机号", @"请输入密码"];
    CGSize size = [LZCTools textSizeWithText:@"测字数" font:kUHSystemFontWithSize(14)];
    
    _phoneNum = [[UITextField alloc] initWithFrame:CGRectMake(30, Height_NavBar+SW(8), SCREEN_WIDTH-60, size.height)];
    [self.view addSubview:_phoneNum];
    _phoneNum.delegate = self;
    _phoneNum.placeholder = @"请输入手机号";
    _phoneNum.textColor = kLightGrayColor;
    _phoneNum.clearButtonMode = UITextFieldViewModeWhileEditing;
    _phoneNum.font = kUHSystemFontWithSize(14);
    [_phoneNum setValue:kLightGrayColor forKeyPath:@"_placeholderLabel.textColor"];
    _phoneNum.leftViewMode=UITextFieldViewModeAlways;
    UIImageView *img = [[UIImageView alloc] initWithImage:IMAGE(@"icon_reg_num")];
    UIView *vv = [[UIView alloc] initWithFrame:CGRectMake(0, 0, img.width+10, _phoneNum.height)];
    [vv addSubview:img];
    _phoneNum.leftView = vv;
    
    
    UIView *line0 = [[UIView alloc] initWithFrame:CGRectMake(20, SW(8)+size.height+5+Height_NavBar, SCREEN_WIDTH-40, 1)];
    [self.view addSubview:line0];
    line0.backgroundColor = kLightGrayColor;
    
    _passWord = [[UITextField alloc] initWithFrame:CGRectMake(30, Height_NavBar+SW(8)+SW(8), SCREEN_WIDTH-60, size.height)];
    [self.view addSubview:_passWord];
    _passWord.delegate = self;
    _passWord.placeholder = @"请输入密码";
    _passWord.textColor = kLightGrayColor;
    _passWord.clearButtonMode = UITextFieldViewModeWhileEditing;
    _passWord.secureTextEntry = YES;
    _passWord.font = kUHSystemFontWithSize(14);
    [_passWord setValue:kLightGrayColor forKeyPath:@"_placeholderLabel.textColor"];
    _passWord.leftViewMode=UITextFieldViewModeAlways;
    UIImageView *img2 = [[UIImageView alloc] initWithImage:IMAGE(@"icon_reg_pwd")];
    UIView *vv2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, img.width+10, _passWord.height)];
    [vv2 addSubview:img2];
    _passWord.leftView = vv2;


    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(20, SW(8)+size.height+5+Height_NavBar+SW(8), SCREEN_WIDTH-40, 1)];
    [self.view addSubview:line];
    line.backgroundColor = kLightGrayColor;
    
    _keepBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:_keepBtn];
    [_keepBtn setImage:IMAGE(@"login_un_tick") forState:UIControlStateNormal];
    [_keepBtn setImage:IMAGE(@"login_tick") forState:UIControlStateSelected];
    [_keepBtn setTitle:@" 记住密码" forState:UIControlStateNormal];
    _keepBtn.titleLabel.font = kUHSystemFontWithSize(12);
    CGSize bsize = [LZCTools textSizeWithText:@" 记住密码" font:kUHSystemFontWithSize(13)];
    _keepBtn.frame = CGRectMake(20, line.y+1+10, bsize.width+bsize.height, bsize.height);
    [_keepBtn setTitleColor:kGrayColor forState:UIControlStateNormal];
    [_keepBtn addTarget:self action:@selector(keepPassClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *forgetBtn = [[UILabel alloc] init];
    [self.view addSubview:forgetBtn];
    forgetBtn.text = @"忘记密码？";
    forgetBtn.textAlignment = NSTextAlignmentCenter;
    forgetBtn.textColor = kGrayColor;
    forgetBtn.font = kUHSystemFontWithSize(12);
    [forgetBtn addTarget:self action:@selector(forgetClick)];
    
    CGSize forgetBtnSize = [LZCTools textSizeWithText:forgetBtn.text font:forgetBtn.font];
    
    forgetBtn.frame = CGRectMake(SCREEN_WIDTH-20-forgetBtnSize.width, line.y+1+10, forgetBtnSize.width, forgetBtnSize.height);
    
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:loginBtn];
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
    CGFloat btnHeight = [LZCTools textSizeWithText:@"字数" font:kUHSystemFontWithSize(14)].height+SW(20);
    loginBtn.frame = CGRectMake(20, _keepBtn.y+_keepBtn.height+20, SCREEN_WIDTH-40, btnHeight);
    loginBtn.backgroundColor = UIMainColorRGB;
    loginBtn.layer.cornerRadius = 5;
    loginBtn.layer.masksToBounds = YES;
    [loginBtn addTarget:self action:@selector(loginClick:) forControlEvents:UIControlEventTouchUpInside];

    
    _register = [[UILabel alloc] init];
    [self.view addSubview:_register];
    _register.textColor = kGrayColor;
    // 不同颜色
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"还没有账号？立刻注册"];
    NSRange redRange = NSMakeRange([[string string] rangeOfString:@"立刻注册"].location, [[string string] rangeOfString:@"立刻注册"].length);
    [string addAttribute:NSForegroundColorAttributeName value:UIMainColorRGB range:redRange];
    [_register setAttributedText:string];
    [_register sizeToFit];
    
    _register.font = kUHSystemFontWithSize(13);
    [_register addTarget:self action:@selector(registerClick:)];
    
    CGSize registerSize = [LZCTools textSizeWithText:_register.text font:_register.font];
    _register.center = CGPointMake(self.view.centerX, loginBtn.y+loginBtn.height+20);
    _register.bounds = CGRectMake(0, 0, registerSize.width, registerSize.height);
    
    
    NSArray *userArr = GetUserDefaule(@"remPass");
    if (userArr) {
        _phoneNum.text = userArr[0];
        _passWord.text = userArr[1];
        _keepBtn.selected = YES;
    }
    
}

#pragma mark ===========响应方法==========
/**
 跳转注册
 */
- (void)registerClick:(id)sender {
    NSLog(@"push注册页面");
    [self.navigationController pushViewController:[[RegisterViewController alloc] init] animated:YES];
}
/**
 记住密码
 */
- (void)keepPassClick:(UIButton *)sender {
    NSLog(@"记住密码");
    sender.selected = !sender.isSelected;
    
    NSLog(@"%d", sender.isSelected);
    
    if (!sender.isSelected) {
        RemoveUserDefaule(@"remPass");
    }
}
/**
 忘记密码
 */
- (void)forgetClick {
    NSLog(@"忘记密码被点击");
    [self.navigationController pushViewController:[[LookingPassViewController alloc] init] animated:YES];
}
/**
 点击用户协议
 */
- (void)agreementClick {
    NSLog(@"点击用户协议");
}

/**
 手机登录
 */
- (void)loginClick:(id)sender {
    
    // 手机号不为空
    if ([NSString isEmpty:_phoneNum.text]) {
        [self presentViewController:[LZCTools alertMessage:@"手机号不可为空"] animated:YES completion:nil];
        return;
    }
    // 手机号码输入验证
    if (![NSString isValidatePhoneNumber:_phoneNum.text]) {
        [self presentViewController:[LZCTools alertMessage:@"您输入的不是手机号"] animated:YES completion:nil];
        return;
    }
    // 密码不为空
    if ([NSString isEmpty:_passWord.text]) {
        [self presentViewController:[LZCTools alertMessage:@"密码不可为空"] animated:YES completion:nil];
        return;
    }
    
    // 记住密码
    if (_keepBtn.isSelected) {
        NSArray *arr = @[_phoneNum.text, _passWord.text];
        SetUserDefaule(arr, @"remPass");
    }
    
    NSDictionary *param = @{@"mobile":_phoneNum.text,
                            @"password":_passWord.text};
	
    [LoginServer loginParam:param];
    
}


#pragma mark ===========代理方法==========
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
